(function ($) {
	var sourceData;
	var perInfo,pageType,nurseid
	var keysArr = [{'key':'OutWard','type':'list','text':'病区'},{'key':'OutNurse','type':'list','text':'姓名'},
		{'key':'OutStDate','type':'date','text':'开始日期'},{'key':'OutEndDate','type':'date','text':'结束日期'},
		{'key':'OutLevel','type':'list','text':'学习级别'},{'key':'OutType','type':'list','text':'学习类型'},
		{'key':'OutIsSpecial','type':'list','text':'是否本专业'},{'key':'OutGroup','type':'list','text':'专业组'},
		{'key':'OutContent','type':'input','text':'学习内容'},{'key':'OutLocation','type':'input','text':'学习地点'},
		{'key':'OutScoreType','type':'list','text':''},{'key':'OutScore','type':'input','text':''},
		{'key':'OutUnit','type':'input','text':'培训单位'},{'key':'OutCost','type':'input','text':''}]
	$.init();
	$.plusReady(function() {
		baseSetup();
	})
	if (!mui.os.plus) {
		baseSetup()
	}
	function submitNewData(){
		blurAllInput()
		var parr = ''
		for (var i = 0; i < keysArr.length; i++) {
			var oneDom = keysArr[i]
			var domId = oneDom['key']
			var dom = $('#'+domId)[0]
			var domValue = dom.getAttribute('data-id')
			var tip = '请选择'
			if (oneDom['type'] == 'date') {
				domValue = dom.innerText
				if (domValue.indexOf('日期') != -1) {
					str = ''
				}
			}else if (oneDom['type'] == 'input') {
				domValue = dom.value
				tip = '请输入'
			}else if (oneDom['type'] == 'list') {
				domValue = dom.getAttribute('data-id')
			}
			if (domValue == '' && oneDom['text']!='') {
				mui.toast(tip + oneDom['text'])
				showEmptyAnimation(domId)
				return
			}
			if (domId == 'OutIsSpecial') {
				domValue = domValue=="Y"?'1':'0'
			}
			parr += domValue +'^'
		}
		if (sourceData) {
			parr += sourceData['rw']
		}
		parr = parr.replace(/__/g,"||")
		//console.log(parr)
		var pdict = {'nurseid':nurseid,'parr':parr}
		var paramDict = {'method':'web.INMHBComm:SaveOutStudy','params':JSON.stringify(pdict),'type':'Method'}
		$.DHCWebService(paramDict, function(result) {
			//console.log(JSON.stringify(result))
			var msg = result['msg']
			if (msg == '1') {
				plus.webview.getWebviewById('../WKPlatformPages/teach/teachList.html').evalJS('detailChanged()')
				mui.back()
			}else{
				mui.toast(msg)
			}
			
		}, function(errorStr) {
			mui.toast('请重试')
		});
	}
	
	function updateUI(){
		for (var i = 0; i < keysArr.length; i++) {
			var oneDom = keysArr[i]
			var keyStr = oneDom['key']
			var dom = $('#'+keyStr)[0]
			if (keyStr == 'OutGroup') {
				keyStr = 'OutSpecialGroup'
			}
			var value = sourceData[keyStr]
			if (oneDom['type'] == 'date') {
				dom.innerText = value
				if (value != '') {
					dom.classList.remove('noTextTip')
				}
			}else if (oneDom['type'] == 'input') {
				dom.value=value
			}else{
				dom.setAttribute('data-id',value)
				value = sourceData[keyStr+'Desc']
				if (keyStr == 'OutNurse') {
					value = sourceData[keyStr+'Name']
				}
				dom.innerText=value
			}
		}
	}
	
	function baseSetup(){
		$('.mui-scroll-wrapper').scroll({
			bounce: true,
			indicators: false, //是否显示滚动条
		});
		var self = plus.webview.currentWebview()
		sourceData = self.data
		pageType = self.pageType
		if (pageType == '专科活动备案') {
			document.body.classList.add('Activity')
		}else if (pageType == '课题专案备案') {
			document.body.classList.add('Case')
		}else{
			document.body.classList.add('Standard')
		}
		perInfo = self.perInfo
		nurseid = perInfo['loginID']
		$('#CreatorName')[0].innerText = perInfo['loginName']
		if (sourceData) { //修改
			updateUI()
		}else{
			$('#OutWard')[0].innerText = perInfo['loginWardDesc']
			$('#OutWard')[0].setAttribute('data-id',perInfo['loginWardId'])
		}
		
		//关闭按钮
		mui('body').on('tap','#closeBtn',mui.back)
		//保存按钮
		mui('body').on('tap','.submitBtn',submitNewData)
		
		//下拉列表
		mui('#UL').on('tap','.listType',downListElementTapped);
		mui('#UL').on('tap','#OutWard',downWardListElementTapped);
		// 日期
		mui('#UL').on('tap','.dateType',selectDate);
		
		mui('#UL').on('tap','.authText',function(){
			blurAllInput()
			var dom = this
			setTimeout(function() {
				toChosePerson(dom.id,'单选','OutWard') //防止切换页面时键盘未隐藏
			}, 100);
		}); //选人
		//window.addEventListener('resize',windowResize)
	}
	//选择日期
	function selectDate(){
		var _self = this;
		plus.nativeUI.pickDate(function(e) {
			var d = e.date;
			var dateStr = d.getFullYear()+'-'
			if (d.getMonth() < 9) {
				dateStr += '0'
			}
			dateStr += (d.getMonth() + 1)+'-'
			if (d.getDate() < 10) {
				dateStr += '0'
			}
			dateStr += d.getDate()
			_self.classList.remove('noTextTip')
			_self.innerText = dateStr;
			resetZkRefresh();
		}, function(e) {}, {});
	}
})(mui)
function selectPerson(domId,params){
	var dom = mui('#'+domId)[0]
	var html='',idStr=''
	for(var i = 0; i < params.length;i++){
		var onePer = params[i]
		idStr = onePer['per']
		html = onePer['perName']
	}
	dom.innerHTML = html;
	dom.setAttribute('data-id',idStr)
}
